const { TableInfo } = require('../utils/data');
const { ServerResponseResult } = require('../utils/result');
const { sqlQueryHomeDatas, sqlQueryHomeDatasAll } = require('../db/sql');
const { selectDatas, fullClose } = require('../utils/tools');

module.exports.getHomeDatasHandler = function (req, res) {
    const cityId = req.body.cityId,
        responseResult = new ServerResponseResult(),
        delayTime = 100;

    if (cityId != 0) {
        // 查询food数据
        selectDatas(responseResult, sqlQueryHomeDatas('food'), 'foodDatas', [cityId]);
        // 查询hotel数据
        selectDatas(responseResult, sqlQueryHomeDatas('hotel'), 'hotelDatas', [cityId]);
        // 查询view数据
        selectDatas(responseResult, sqlQueryHomeDatas('view'), 'viewDatas', [cityId]);
        // 查询ktv数据
        selectDatas(responseResult, sqlQueryHomeDatas('ktv'), 'ktvDatas', [cityId]);
        // 查询massage数据
        selectDatas(responseResult, sqlQueryHomeDatas('massage'), 'massageDatas', [cityId]);
    } else {
        // 随机生成两个id
        const id1 = fullClose(TableInfo.minIdNum, TableInfo.maxIdNum),
            id2 = fullClose(TableInfo.minIdNum, TableInfo.maxIdNum),
            id3 = fullClose(TableInfo.minIdNum, TableInfo.maxIdNum),
            id4 = fullClose(TableInfo.minIdNum, TableInfo.maxIdNum);

        // 查询food数据
        selectDatas(responseResult, sqlQueryHomeDatasAll('food'), 'foodDatas', [id1, id2]);
        // 查询hotel数据
        selectDatas(responseResult, sqlQueryHomeDatasAll('hotel'), 'hotelDatas', [id2, id3]);
        // 查询view数据
        selectDatas(responseResult, sqlQueryHomeDatasAll('view'), 'viewDatas', [id3, id4]);
        // 查询ktv数据
        selectDatas(responseResult, sqlQueryHomeDatasAll('ktv'), 'ktvDatas', [id1, id2]);
        // 查询massage数据
        selectDatas(responseResult, sqlQueryHomeDatasAll('massage'), 'massageDatas', [id3, id4]);
    }

    setTimeout(function () {
        if (responseResult.foodDatas.length > 0 ||
            responseResult.hotelDatas.length > 0 ||
            responseResult.massageDatas.length > 0 ||
            responseResult.ktvDatas.length > 0 ||
            responseResult.viewDatas.length > 0) {
            responseResult.statusText = 'ok'
        } else {
            responseResult.statusText = 'No Datas'
        }
        return res.send(responseResult);
    }, 2 * delayTime);

}
